<?php

class Auth extends CI_Controller {
	function Auth()
	{
		parent::__construct();	
		$this->load->model('users_model', 'users_tbl');
		$this->load->model('student_model');
		$this->load->library('Retrieve');
	}
	function getPath($action = ''){
		return array('controller' => strtolower(__CLASS__), 'action' => strtolower($action));
	}
	function index()
	{
		$mylogin = $this->session->userdata('mylogin');
		if(empty($mylogin)) {
			$this->login();
		} else {
			redirect($this->config->item('admin_base').'users/index','location');
			exit;
		}
	}
	
	function login(){
		
		$view = array();
		
		$login = $this->input->post('login');
		$username = $this->input->post('ap_user');
		$password = $this->input->post('ap_pwd');
		$status = $this->student_model->login($username, $password);
		if($status) {
			$mylogin = $this->session->userdata('mylogin');
			if(empty($_REQUEST['surl'])) 
				redirect('member/upload_form/index','location');
			else 
				redirect($_REQUEST['surl'],'location');
			exit;
		}
		$view['surl'] = $_REQUEST['surl'];
		$this->layout->getView('users/login', $view);
	}
	
	function logout(){
		$this->session->sess_destroy();
		redirect('auth/index','location');
		exit;
	}
	
	function changepwd(){
		$mylogin = $this->session->userdata('mylogin');
		$save = $this->input->post('save');
		if(!empty($save)) {
			$name = $this->input->post('name');
			$password = $this->input->post('mypassword');
			$confpassword = $this->input->post('myconfpassword');
			if($password == $confpassword && !empty($password)) {
				$data = array();
				$data['name'] = $name;
				$data['mypassword'] = $password;
				$status = $this->users_tbl->changepwd($mylogin['id'], $data);
			}
		}
		$obj = $this->users_tbl->getUsers(0,1,array('id' => $mylogin['id']));
		$user = $obj->records[0];
		$view = $this->getPath();
		$view['title'] = "Change Password";
		$view['page_title'] = "Change Password";
		$view['subtitle']	= 'Change Password';
		$view['user'] = $user;
		$view['rurl'] = $_REQUEST['rurl'];
		$view['id'] = $mylogin['id'];
		if($status) $view['msg'] = 'Password Changed Successfully'; 
		$this->layout->adminview('users/changepwd', $view);
		//$this->layout->adminview('users/chg', $view);

	}
	
	function cancel() {
		if(!empty($_REQUEST['rurl'])) {
			redirect($_REQUEST['rurl'],'location');
		} else {
			redirect($this->config->item('admin_base').'users/index','location');	
		}
		
		exit;	
	}
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */